"""
Problem 48: https://projecteuler.net/problem=48

Self Powers

The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.

Find the last ten digits of the series, 
1^1 + 2^2 + 3^3 + ... + 1000^1000.
"""

# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/5/15
'''


def addmod(x: int, y: int, m: int) -> int:
    return (x+y) % m


def productmod(x: int, y: int, m: int) -> int:
    return x*y % m


def powermod(x: int, n: int, m: int) -> int:
    import functools
    return functools.reduce(lambda x, y: (x*y) % m, [x]*n)


def solution(n: int = 1000, d: int = 10) -> str:
    '''
   ( 1^1 + 2^2 + 3^3 + ... + 1000^1000) mod 10^10

    >>> print(solution(10))  # 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317
    0405071317
    '''
    m = 10**d

    import functools
    total = functools.reduce(lambda x,y:addmod(x,y,m), 
                             [powermod(i,i,m) for i in range(1, n+1)])
    return str(total).zfill(d)


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=False)

    print(solution())
    # 9110846700
